use sqlx::MySqlPool;

#[derive(Debug, sqlx::FromRow, Clone)]
pub struct AiTargetType {
    pub id: Option<u32>,
    pub head_img: String,
}

impl AiTargetType {
    pub async fn delete(&self, pool: &MySqlPool) -> Result<u64, sqlx::Error> {
        let result = if let Some(id) = self.id {
            sqlx::query!("DELETE FROM ai_target_type WHERE id = ?", id)
                .execute(pool)
                .await?
        } else {
            return Ok(0);
        };
        Ok(result.rows_affected())
    }
    // 查询列表
    pub async fn query_list(pool: &MySqlPool) -> Result<Vec<Self>, sqlx::Error> {
        let rows = sqlx::query!("SELECT id, head_img FROM ai_target_type")
            .fetch_all(pool)
            .await?;

        // 转换为 Vec<AiSource>
        let list = rows
            .into_iter()
            .map(|row| Self {
                id: row.id.into(),
                head_img: row.head_img,
            })
            .collect();

        Ok(list)
    }
}
